﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZP.Common;
using ZP.DTO.Common;
using ZP.DTO.fz_sys_menu;
using ZP.IBLL;

namespace ZCSBZPJ.Controllers
{
    public class FZMenuController : BaseController
    {
        //public FZMenuController(Ifz_sys_menuService _fz_sys_menuService)
        //{
        //    this._fz_sys_menuService = _fz_sys_menuService;
        //}

        /// <summary>
        /// 获得菜单集合TreeView
        /// </summary>
        /// <returns></returns>
        public JsonResult GetMenuTreeJson()
        {
            List<fz_sys_menuResultDTO> sysMenuList = _fz_sys_menuService.GetSysMenu().Where(p => p.state == ZPFlagType.YES).ToList();
            List<ZPMenuNodeModel> nodeList = GetTreeJson(sysMenuList);
            return Json(nodeList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 一级Menu转换TreeNode
        /// </summary>
        /// <returns></returns>
        private List<ZPMenuNodeModel> GetTreeJson(List<fz_sys_menuResultDTO> sysMenuList)
        {
            List<ZPMenuNodeModel> nodeList = new List<ZPMenuNodeModel>();
            List<fz_sys_menuResultDTO> oneNode = sysMenuList.Where(p => p.upId == 0).ToList();//根菜单
            List<fz_sys_menuResultDTO> childrenNode = null;//子菜单
            int tid = 0;
            foreach (fz_sys_menuResultDTO one in oneNode)
            {
                childrenNode = sysMenuList.Where(p => p.upId != 0).ToList();
                //nodeList.Add(new ZPMenuNodeModel(tid, one.id, one.upId, one.name, one.path, ChildrenMenuToTreeJson(childrenNode, one.id, ref tid)));//bootstrap tree
                //nodeList.Add(new ZPMenuNodeModel("", one.name, one.id, false, false, false, false, ChildrenMenuToTreeJson(childrenNode, one.id, ref tid)));//layui 插件eleTree
                nodeList.Add(new ZPMenuNodeModel(one.name, one.id, "", one.path, false, false, false, ChildrenMenuToTreeJson(childrenNode, one.id, ref tid)));//layui tree
            }
            return nodeList;
        }

        /// <summary>
        /// 子菜单转换TreeNode
        /// </summary>
        /// <returns></returns>
        private List<ZPMenuNodeModel> ChildrenMenuToTreeJson(List<fz_sys_menuResultDTO> childrenNode, long upId, ref int tid)
        {
            List<ZPMenuNodeModel> nodeList = new List<ZPMenuNodeModel>();
            ZPMenuNodeModel node = null;
            foreach (fz_sys_menuResultDTO child in childrenNode)
            {
                if (child.upId != upId)
                    continue;

                tid++;
                //node = new ZPMenuNodeModel(tid, child.id, child.upId, child.name, child.path, ChildrenMenuToTreeJson(childrenNode, child.id, ref tid));//bootstrap tree
                //node = new ZPMenuNodeModel(upId.ToString(), child.name, child.id, false, true, false, false, ChildrenMenuToTreeJson(childrenNode, child.id, ref tid));//layui 插件eleTree
                node = new ZPMenuNodeModel(child.name, child.id, "", child.path, false, false, false, ChildrenMenuToTreeJson(childrenNode, child.id, ref tid));
                nodeList.Add(node);//layui tree
            }
            return nodeList;
        }
    }
}